package cn.wolfcode.trip.base.util;

import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/*
    用户上下文工具类:作用把用户存入session
    从session中拿到登录的用户
*/
public abstract class UserContext {
    private static HttpSession getSession() {
        return ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes()))
                .getRequest().getSession();
    }

    public static void setCurrentUser(User user) {
        getSession().setAttribute("USER_IN_SESSION", user);
    }

    public static User getCurrentUser() {
        return (User) getSession().getAttribute("USER_IN_SESSION");
    }

    public static void invalidate(){
        getSession().invalidate();
    }
}
